<template>  
  <div class="system-cameraRole-add">
    <!-- 添加或修改国标设备和角色对应对话框 -->
    <el-dialog v-model="isShowDialog" width="769px" :close-on-click-modal="false" :destroy-on-close="true">
      <template #header>
        <div v-drag="['.system-cameraRole-add .el-dialog', '.system-cameraRole-add .el-dialog__header']">添加国标设备和角色对应</div>
      </template>
      <el-form ref="formRef" :model="formData" :rules="rules" label-width="90px">        
        <el-form-item label="国标id" prop="gbId">
          <el-input v-model="formData.gbId" placeholder="请输入国标id" />
        </el-form-item>        
        <el-form-item label="角色id" prop="roleId">
          <el-input v-model="formData.roleId" placeholder="请输入角色id" />
        </el-form-item>       
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="onSubmit">确 定</el-button>
          <el-button @click="onCancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts">
import { reactive, toRefs, defineComponent,ref,unref,getCurrentInstance } from 'vue';
import {ElMessageBox, ElMessage, FormInstance,UploadProps} from 'element-plus';
import {
  listCameraRole,
  getCameraRole,
  delCameraRole,
  addCameraRole,
  updateCameraRole,  
} from "/@/api/system/cameraRole";
import {
  CameraRoleTableColumns,
  CameraRoleInfoData,
  CameraRoleTableDataState,
  CameraRoleEditState,  
} from "/@/views/system/cameraRole/list/component/model"
export default defineComponent({
  name:"apiV1SystemCameraRoleEdit",
  components:{    
  },
  props:{    
  },
  setup(props,{emit}) {    
    const {proxy} = <any>getCurrentInstance()
    const formRef = ref<HTMLElement | null>(null);
    const menuRef = ref();    
    const state = reactive<CameraRoleEditState>({
      loading:false,
      isShowDialog: false,
      formData: {        
        id: undefined,        
        gbId: undefined,        
        roleId: undefined,        
      },
      // 表单校验
      rules: {        
        id : [
            { required: true, message: "不能为空", trigger: "blur" }
        ],        
        gbId : [
            { required: true, message: "国标id不能为空", trigger: "blur" }
        ],        
        roleId : [
            { required: true, message: "角色id不能为空", trigger: "blur" }
        ],        
      }
    });
    // 打开弹窗
    const openDialog = () => {
      resetForm();
      state.isShowDialog = true;
    };
    // 关闭弹窗
    const closeDialog = () => {
      state.isShowDialog = false;
    };
    // 取消
    const onCancel = () => {
      closeDialog();
    };
    // 提交
    const onSubmit = () => {
      const formWrap = unref(formRef) as any;
      if (!formWrap) return;
      formWrap.validate((valid: boolean) => {
        if (valid) {
          state.loading = true;
          //添加
          addCameraRole(state.formData).then(()=>{
              ElMessage.success('添加成功');
              closeDialog(); // 关闭弹窗
              emit('cameraRoleList')
            }).finally(()=>{
              state.loading = false;
            })
        }
      });
    };
    const resetForm = ()=>{
      state.formData = {        
        id: undefined,        
        gbId: undefined,        
        roleId: undefined,        
      }
    };    
    return {
      proxy,
      openDialog,
      closeDialog,
      onCancel,
      onSubmit,
      menuRef,
      formRef,      
      ...toRefs(state),
    };
  }
})
</script>
<style scoped>  
</style>